﻿using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;


namespace SJKP.PDFConversion.Service
{
    [Guid("0d619b0f-6a7c-490a-8910-99768c6bf65e")]
    public class OCRService : SPIisWebService, IServiceAdministration
    {
        public OCRService()
        {
        }

        public OCRService(SPFarm farm) : base(farm)
        {
            
        }

        public Type[] GetApplicationTypes()
        {
            return new Type[] {typeof(OCRServiceApplication)};
        }

        public SPPersistedTypeDescription GetApplicationTypeDescription(Type serviceApplicationType)
        {
            if (typeof(OCRServiceApplication) != serviceApplicationType)
                throw new NotSupportedException();

            return new SPPersistedTypeDescription("OCR Service", "Service for OCR processing pdf files");
        }

        public override SPAdministrationLink GetCreateApplicationLink(Type serviceApplicationType)
        {
            return new SPAdministrationLink("/_admin/SJKP.OCR/Create.aspx");
        }

        public override SPCreateApplicationOptions GetCreateApplicationOptions(Type serviceApplicationType)
        {
            return SPCreateApplicationOptions.None;
        }
        #region Farm Wizard Configuration Methods (Not fully implemented)
        public SPServiceApplication CreateApplication(string name, Type serviceApplicationType, SPServiceProvisioningContext provisioningContext)
        {
            Validate(name, serviceApplicationType, provisioningContext);
            return CreateServiceApplication(name, serviceApplicationType, provisioningContext);
        }
        
        public SPServiceApplicationProxy CreateProxy(string name, SPServiceApplication serviceApplication, SPServiceProvisioningContext provisioningContext)
        {
            Validate(name, serviceApplication, provisioningContext);
            return null;//CreateServiceApplicationProxy()
        }

        private static void Validate<T>(string name, T serviceApplication, SPServiceProvisioningContext provisioningContext) where T : class
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "name not specified");
            }
            if (serviceApplication == null)
            {
                throw new ArgumentNullException("serviceApplication");
            }
            if(serviceApplication.GetType() != typeof(T))
            {
                throw new ArgumentException("serviceApplication");
            }
        }

        private OCRServiceApplication CreateServiceApplication(string name, Type serviceApplicationType, SPServiceProvisioningContext provisioningContext)
        {
            OCRServiceApplication serviceApp = this.Farm.GetObject(name, Id, serviceApplicationType) as OCRServiceApplication;
            if (serviceApp == null)
            {
                serviceApp = new OCRServiceApplication(name, this, provisioningContext.IisWebServiceApplicationPool);
                serviceApp.Update();

                serviceApp.AddServiceEndpoint("http", SPIisWebServiceBindingType.Http);                
            }
            return serviceApp;
        }
        #endregion
    }


}
